package com.golden.procedure.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.golden.procedure.dao.WorkOrderBookMapper;
import com.golden.procedure.dao.WorkOrderMapper;
import com.golden.procedure.dao.WorkOrderPhotographyMapper;
import com.golden.procedure.dao.WorkSeatSchedulingMapper;
import com.golden.procedure.dao.WorkSiteSeatMapper;
import com.golden.procedure.domain.WorkCommentDO;
import com.golden.procedure.domain.WorkOrderBookDO;
import com.golden.procedure.domain.WorkOrderDO;
import com.golden.procedure.domain.WorkOrderExchangeDO;
import com.golden.procedure.domain.WorkOrderPhotographyDO;
import com.golden.procedure.domain.WorkSeatSchedulingDO;
import com.golden.procedure.domain.WorkTaskHistoryDO;
import com.golden.procedure.entity.WorkOrder;
import com.golden.procedure.entity.WorkOrderBook;
import com.golden.procedure.entity.WorkOrderBookExample;
import com.golden.procedure.entity.WorkOrderExample;
import com.golden.procedure.entity.WorkOrderPhotography;
import com.golden.procedure.entity.WorkOrderPhotographyExample;
import com.golden.procedure.entity.WorkSeatScheduling;
import com.golden.procedure.entity.WorkSeatSchedulingExample;
import com.golden.procedure.entity.WorkSiteSeat;
import com.golden.procedure.query.WorkOrderBookQueryDO;
import com.golden.procedure.query.WorkOrderQueryDO;
import com.golden.procedure.result.ResultDO;
import com.golden.procedure.result.ResultSupport;
import com.golden.procedure.service.ISiteService;
import com.golden.procedure.service.IWorkCommentService;
import com.golden.procedure.service.IWorkOrderBookService;
import com.golden.procedure.service.IWorkOrderService;
import com.golden.procedure.service.IWorkPhotographyService;
import com.golden.procedure.service.IWorkSeatSchedulingService;
import com.golden.procedure.service.IWorkTaskHistoryService;
import com.golden.procedure.util.BeanUtilsExtends;
import com.golden.procedure.util.BonConstant;
import com.golden.procedure.util.TaskDefKey;

/**
 * 工单预约服务 <b>Description:</b><br>
 * 
 * @author Reading.Reagan</a>
 * @version 1.0
 * @Note <b>ProjectName:</b> procedure <br>
 *       <b>PackageName:</b> com.golden.procedure.service.impl <br>
 *       <b>ClassName:</b> WorkOrderYYService <br>
 *       <b>Date:</b> 2018年9月21日 上午11:43:45
 */

@Service("workOrderBookService")
public class WorkOrderBookService extends BaseService implements IWorkOrderBookService {

	@Autowired
	WorkOrderMapper workOrderMapper;
	@Autowired
	WorkOrderBookMapper workOrderBookMapper;
	@Autowired
	WorkOrderPhotographyMapper workOrderPhotographyMapper;
	@Autowired
	WorkSiteSeatMapper workSiteSeatMapper;
	@Autowired
	WorkSeatSchedulingMapper workSeatSchedulingMapper;
	@Autowired
	ISiteService sietService;
	@Autowired
	IWorkSeatSchedulingService workSeatSchedulingService;
	@Autowired
	IWorkOrderService workOrderService;
	@Autowired
	IWorkPhotographyService workPhotographyService;
	@Autowired
	IWorkCommentService workCommentService;
	@Autowired
	IWorkTaskHistoryService workTaskHistoryService;

	/**
	 * 查询工单列表 <b>Description:</b><br>
	 * 
	 * @param queryDO
	 * @return ResultDO
	 * @Note <b>Author:</b> Reading.Reagan <br>
	 *       <b>Date:</b> 2018年9月19日 下午4:10:05 <br>
	 *       <b>Version:</b> 1.0
	 */
	@Override
	public ResultDO query(WorkOrderBookQueryDO queryDO) {
		ResultDO result = new ResultSupport();

		WorkOrderBookExample example = new WorkOrderBookExample();
		WorkOrderBookExample.Criteria criteria = example.createCriteria();
		if (!isNumberInValid(queryDO.getSiteSeatID())) {
			criteria.andSiteSeatIDEqualTo(queryDO.getSiteSeatID());
		}
		if (!isNumberInValid(queryDO.getSeatID())) {
			criteria.andSeatIDEqualTo(queryDO.getSeatID());
		}
		if (!isNumberInValid(queryDO.getWorkOrderID())) {
			criteria.andWorkOrderIDEqualTo(queryDO.getWorkOrderID());
		}
		criteria.andIsDeleteEqualTo(BonConstant.DB_NORMAL);
		criteria.andStatusNotEqualTo(BonConstant.WORKORDER_STATUS_CANCLE); // 工单预约状态不等于取消
		List<WorkOrderBook> list = workOrderBookMapper.selectByExample(example);

		List<WorkOrderBookDO> doList = new ArrayList<WorkOrderBookDO>();
		if (null != list && list.size() > 0) {
			for (WorkOrderBook data : list) {
				WorkOrderBookDO dst = getWorkOrderBookDO(data);
				if (null != dst) {
					// 查询工单信息
					if (StringUtils.isNotEmpty(queryDO.getNameOrTel())) {
						WorkOrderQueryDO query1 = new WorkOrderQueryDO();
						query1.setId(data.getWorkOrderID());
						if (StringUtils.isNotEmpty(queryDO.getNameOrTel())) {
							query1.setNameOrTel(queryDO.getNameOrTel());
						}
						ResultDO result1 = workOrderService.query(query1);
						if (result1.isSuccess()) {
							List<WorkOrderDO> list2 = (List<WorkOrderDO>) result1.getModel(ResultDO.FIRST_MODEL_KEY);
							if (null != list2 && list2.size() > 0) {
								dst.setWorkOrderDO(list2.get(0));
							}
						}
					} else {
						ResultDO result1 = workOrderService.get(data.getWorkOrderID());
						if (result1.isSuccess()) {
							dst.setWorkOrderDO((WorkOrderDO) result1.getModel(ResultDO.FIRST_MODEL_KEY));
						} else {
							// 工单查询失败
							result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
							result.setErrorMsg("工单信息查询失败：id=" + data.getWorkOrderID());
							result.setSuccess(false);
							return result;
						}
					}
					doList.add(dst);
				}
			}
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, doList);
		return result;
	}

	@Override
	public ResultDO queryBooked(WorkOrderBookQueryDO queryDO) {

		ResultDO result = new ResultSupport();
		// 接口返回数据对象列表
		List<WorkSeatSchedulingDO> doList = new ArrayList<WorkSeatSchedulingDO>();
		// 1. 参数检查
		if (isNumberInValid(queryDO.getSiteID()) || StringUtils.isEmpty(queryDO.getSchedulingDateStr())) {
			result.setErrorCode(ResultDO.PARAM_ERROR);
			result.setErrorMsg("参数错误：摄控日期为空");
			result.setSuccess(false);
			return result;
		}

		// 查询满足条件的拍摄记录
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("siteID", queryDO.getSiteID());

		Date schedulingDate = new Date(Long.parseLong(queryDO.getSchedulingDateStr()));
		params.put("schedulingDate", schedulingDate);

		if (StringUtils.isNotEmpty(queryDO.getProvince())) {
			params.put("province", queryDO.getProvince());
		}
		if (StringUtils.isNotEmpty(queryDO.getCity())) {
			params.put("city", queryDO.getCity());
		}
		if (StringUtils.isNotEmpty(queryDO.getDistrict())) {
			params.put("district", queryDO.getDistrict());
		}
		if (StringUtils.isNotEmpty(queryDO.getPlanStartOnStr())) {
			params.put("planStart", queryDO.getPlanStartOnStr());
			params.put("planEnd", queryDO.getPlanEndOnStr());
		}
		if (StringUtils.isNotEmpty(queryDO.getNameOrTel())) {
			params.put("nameOrTel", "%" + queryDO.getNameOrTel() + "%");
		}

		if (queryDO.isPage()) {
			params.put("limitByPage", queryDO.getStart() + "," + queryDO.getPageRows());
			int count = workOrderBookMapper.countBookedList(params);
			result.setModel(ResultDO.SECOND_MODEL_KEY, count);
		}

		List<WorkOrderBook> list = workOrderBookMapper.selectBookedList(params);
		if (null == list || list.size() < 1) {
			result.setModel(ResultDO.FIRST_MODEL_KEY, doList);
			return result;
		}

		for (WorkOrderBook book : list) {
			WorkOrderBookDO bookDO = getWorkOrderBookDO(book);
			// 查询工单
			WorkOrder order = workOrderMapper.selectByPrimaryKey(book.getWorkOrderID());
			bookDO.setWorkOrderDO(getWorkOrderDO(order));
			// 查询席位
			WorkSeatScheduling seat = workSeatSchedulingMapper.selectByPrimaryKey(book.getSeatID());
			WorkSeatSchedulingDO seatSchedulingDO = getWorkSeatSchedulingDO(seat);
			seatSchedulingDO.setSiteName(sietService.getName(seat.getSiteID()));
			seatSchedulingDO.setWorkOrderBookDO(bookDO);

			// 查询工位
			WorkSiteSeat siteSeat = workSiteSeatMapper.selectByPrimaryKey(book.getSiteSeatID());
			seatSchedulingDO.setRegion(siteSeat.getProvince(), siteSeat.getCity(), siteSeat.getDistrict());
			doList.add(seatSchedulingDO);
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, doList);
		return result;
	}

	/**
	 * 预约工单 <b>Description: 上摄控任务节点，同时保存任务记录</b><br>
	 * 
	 * @param domain
	 * @return ResultDO
	 * @Note <b>Author:</b> Reading.Reagan <br>
	 *       <b>Date:</b> 2018年9月20日 下午3:43:42 <br>
	 *       <b>Version:</b> 1.0
	 */
	@Override
	public ResultDO booking(WorkOrderBookDO domain) {
		// 订单上摄控
		ResultDO result = new ResultSupport();
		// 1.数据验证
		WorkOrderBook entity = new WorkOrderBook();
		if (!BeanUtilsExtends.copyProperties(entity, domain)) {
			result.setErrorCode(ResultDO.PARAM_ERROR);
			result.setErrorMsg("数据格式转换错误");
			result.setSuccess(false);
			return result;
		}
		if (isNumberInValid(domain.getSeatID()) || isNumberInValid(domain.getWorkOrderID())) {
			result.setErrorCode(ResultDO.PARAM_ERROR);
			result.setErrorMsg("参数错误");
			result.setSuccess(false);
			return result;
		}
		// 2.查询工单
		ResultDO res1 = workOrderService.get(domain.getWorkOrderID());
		if (!res1.isSuccess()) {
			result.setErrorCode(ResultDO.PARAM_ERROR);
			result.setErrorMsg("工单不存在");
			result.setSuccess(false);
			return result;
		}
		WorkOrderDO workOrderDO = (WorkOrderDO) res1.getModel(ResultDO.FIRST_MODEL_KEY);
		// 判断此工单是否已预约
		if (!TaskDefKey.ORDER_SCHEDULE.getKey().equals(workOrderDO.getTaskDefID())) {
			result.setErrorCode(ResultDO.PARAM_ERROR);
			result.setErrorMsg("该工单已预约拍摄，请勿重复预约");
			result.setSuccess(false);
			return result;
		}
		// 3.查询席位、工位等信息
		ResultDO res2 = workSeatSchedulingService.get(domain.getSeatID());
		if (!res2.isSuccess()) {
			result.setErrorCode(ResultDO.PARAM_ERROR);
			result.setErrorMsg("席位不存在");
			result.setSuccess(false);
			return result;
		}
		WorkSeatSchedulingDO seatDO = (WorkSeatSchedulingDO) res2.getModel(ResultDO.FIRST_MODEL_KEY);
		// 判断此工位是否已被占用
		if (seatDO.getStatus() == BonConstant.SEAT_STATUS_USED) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("该席位已被占用");
			result.setSuccess(false);
			return result;
		}

		// 4.设置工单、工位信息,写入预约表
		entity.setProcessInstanceID(workOrderDO.getProcessInstanceID());
		entity.setTaskDefID(TaskDefKey.ORDER_SCHEDULE.getKey());
		entity.setTaskID(""); // TODO: 从activiti中获取
		entity.setSiteSeatID(seatDO.getSiteSeatID());
		entity.setSchedulingDate(seatDO.getSchedulingDate());
		entity.setOrderID(workOrderDO.getOrderID());
		entity.setOrderProductSceenID(workOrderDO.getOrderProductSceenID());
		entity.setOrderProductSceenName(workOrderDO.getOrderProductSceenName());
		entity.setStatus(BonConstant.STATUS_TRUE);
		entity.setEmployeeID(domain.getAuthorID());
		entity.setEmployeeName(domain.getAuthor());
		entity.setIsDelete(BonConstant.DB_NORMAL);
		entity.setCreatetime(Calendar.getInstance().getTime());

		workOrderBookMapper.insert(entity);

		// 5.更新席位状态为已占用
		WorkSeatSchedulingDO seatSchedulingDO = new WorkSeatSchedulingDO();
		seatSchedulingDO.setId(seatDO.getId());
		seatSchedulingDO.setStatus(BonConstant.SEAT_STATUS_USED);
		seatSchedulingDO.setModifiID(domain.getAuthorID());
		seatSchedulingDO.setModifi(domain.getAuthor());
		ResultDO result2 = workSeatSchedulingService.edit(seatSchedulingDO);
		if (!result2.isSuccess()) {
			result.setErrorCode(ResultDO.PARAM_ERROR);
			result.setErrorMsg("订单拍摄预约失败");
			result.setSuccess(false);
			return result;
		}

		// TODO: 6.完成第一个节点任务进入下一流程
		// List<Task> taskList =
		// taskService.createTaskQuery().processInstanceId(processInstanceID).taskDefinitionKey(TaskDefKey.ORDER_SCHEDULE.getKey())
		// .taskAssignee("test").active().list();
		// for (Task task : taskList) {
		// Map<String, Object> map = new HashMap<String, Object>();
		// map.put("inputUser", userName);
		// clientAPI.complete(task.getId(), null);
		// }

		// 7.写入工单任务记录表、工单执行备注表
		// 备注表
		WorkCommentDO commentDO = new WorkCommentDO();
		commentDO.setWorkOrderID(workOrderDO.getId());
		commentDO.setProcessInstanceID(workOrderDO.getProcessInstanceID());
		commentDO.setTaskDefID(TaskDefKey.ORDER_SCHEDULE.getKey()); // 订单上摄控
		commentDO.setTaskName(TaskDefKey.ORDER_SCHEDULE.getName());
		commentDO.setTaskID(""); // 从acitivti获取
		commentDO.setType(BonConstant.COMMENT_TYPE_EVENT); // 0备注；1事件
		commentDO.setRemark("订单预约拍摄：" + new SimpleDateFormat("yyyy-MM-dd").format(seatDO.getSchedulingDate()) + " "
				+ seatDO.getPlanStartOnStr());
		commentDO.setAuthorID(domain.getAuthorID());
		commentDO.setAuthor(domain.getAuthor());
		workCommentService.add(commentDO);

		// 工单任务记录表
		WorkTaskHistoryDO taskHistoryDO = new WorkTaskHistoryDO();
		taskHistoryDO.setWorkOrderID(workOrderDO.getId());
		taskHistoryDO.setProcessInstanceID(workOrderDO.getProcessInstanceID());
		taskHistoryDO.setTaskDefID(TaskDefKey.ORDER_SCHEDULE.getKey()); // 订单上摄控
		taskHistoryDO.setTaskName(TaskDefKey.ORDER_SCHEDULE.getName());
		taskHistoryDO.setTaskID(""); // 从acitivti获取
		taskHistoryDO.setSchedulingdate(seatDO.getSchedulingDate());
		taskHistoryDO.setEndOn(Calendar.getInstance().getTime());
		taskHistoryDO.setAuthorID(domain.getAuthorID());
		taskHistoryDO.setAuthor(domain.getAuthor());
		taskHistoryDO.setStatus(BonConstant.STATUS_TRUE);
		workTaskHistoryService.add(taskHistoryDO);

		// 8.更新工单表(更新当前任务)
		WorkOrder workOrderupdate = new WorkOrder();
		workOrderupdate.setId(workOrderDO.getId());
		workOrderupdate.setTaskDefID(TaskDefKey.UPLOAD_PHOTO.getKey());
		// 是否是改期（订单改期标志，且最近一次订单状态为改期）
		if (null != workOrderDO.getIsChangeDate() && workOrderDO.getIsChangeDate() == BonConstant.STATUS_TRUE
				&& workOrderDO.getOrderStatus() == BonConstant.ORDER_STATUS_CHANGEDATE) {
			// 设置改期日期
			workOrderupdate.setChangeDate(seatSchedulingDO.getSchedulingDate());
		}
		int ret = workOrderMapper.updateByPrimaryKeySelective(workOrderupdate);
		if (ret < 1) {
			result.setErrorCode(ResultDO.PARAM_ERROR);
			result.setErrorMsg("订单拍摄预约失败");
			result.setSuccess(false);
			return result;
		}

		// 9.写入拍摄表（待拍摄）；
		WorkOrderPhotographyDO photographyDO = new WorkOrderPhotographyDO();
		photographyDO.setWorkOrderID(workOrderDO.getId());
		photographyDO.setProcessInstanceID(workOrderDO.getProcessInstanceID());
		photographyDO.setTaskDefID(TaskDefKey.UPLOAD_PHOTO.getKey()); // 任务为：拍摄
		photographyDO.setTaskID(""); // 从acitivti获取
		photographyDO.setPlanDate(seatDO.getSchedulingDate());
		photographyDO.setShotRatio(workOrderDO.getShotRatio());
		photographyDO.setShotNum(workOrderDO.getShotNum());
		photographyDO.setSiteID(seatDO.getSiteID());
		photographyDO.setSiteSeatID(seatDO.getSiteSeatID());
		photographyDO.setSeatID(seatDO.getId());
		photographyDO.setAuthorID(domain.getAuthorID());
		photographyDO.setAuthor(domain.getAuthor());
		ResultDO resultDO = workPhotographyService.add(photographyDO);
		if (!resultDO.isSuccess()) {
			result.setErrorCode(ResultDO.PARAM_ERROR);
			result.setErrorMsg("订单拍摄预约初始化失败");
			result.setSuccess(false);
			return result;
		}
		// 11.发送预约短信
		if (domain.getSmsFlag() == 1) {
			// TODO: 发送短信
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, workOrderDO);
		return result;
	}

	/**
	 * 订单改期 <b>Description:</b><br>
	 * 
	 * @param WorkOrderDO
	 *            domain
	 * @return ResultDO
	 * @Note <b>Author:</b> Reading.Reagan <br>
	 *       <b>Date:</b> 2018年9月20日 下午6:14:42 <br>
	 *       <b>Version:</b> 1.0
	 */
	@Override
	public ResultDO changeDate(WorkOrderBookDO domain) {
		// 订单改期
		ResultDO result = new ResultSupport();
		// 1.数据验证
		if (isNumberInValid(domain.getId())) {
			result.setErrorCode(ResultDO.PARAM_ERROR);
			result.setErrorMsg("参数错误");
			result.setSuccess(false);
			return result;
		}
		// 查询工单预约表
		WorkOrderBook workOrderBook = workOrderBookMapper.selectByPrimaryKey(domain.getId());
		if (null == workOrderBook) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("未找到该席位的预订信息");
			result.setSuccess(false);
			return result;
		}
		// 更新预约表（删除本次预约）
		workOrderBook.setStatus(BonConstant.STATUS_FALSE); // 取消本次预约
		workOrderBook.setIsDelete(BonConstant.DB_DELETE);
		workOrderBook.setModifiID(domain.getModifiID());
		workOrderBook.setModifi(domain.getModifi());
		workOrderBook.setModifitime(Calendar.getInstance().getTime());

		int ret = workOrderBookMapper.updateByPrimaryKeySelective(workOrderBook);
		if (ret < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("工单预订改期失败");
			result.setSuccess(false);
			return result;
		}

		// 更新席位表（修改席位为可用）
		WorkSeatSchedulingDO seatScheduling = new WorkSeatSchedulingDO();
		seatScheduling.setId(workOrderBook.getSeatID());
		seatScheduling.setStatus(BonConstant.SEAT_STATUS_ENABLE); // 席位状态为可用
		seatScheduling.setModifiID(domain.getModifiID());
		seatScheduling.setModifi(domain.getModifi());
		ResultDO resultDO = workSeatSchedulingService.edit(seatScheduling);
		if (!resultDO.isSuccess()) {
			return resultDO;
		}

		// 更新工单表状态（工单状态为改期）
		WorkOrderDO workOrder = new WorkOrderDO();
		workOrder.setId(workOrderBook.getWorkOrderID());
		workOrder.setTaskDefID(TaskDefKey.ORDER_SCHEDULE.getKey()); // 设置当前待执行任务Key(工单预约)
		workOrder.setIsChangeDate(BonConstant.STATUS_TRUE); // 改期标志
		workOrder.setOrderStatus(BonConstant.ORDER_STATUS_CHANGEDATE); // 改期订单
		workOrder.setModifiID(domain.getModifiID());
		workOrder.setModifi(domain.getModifi());
		resultDO = workOrderService.edit(workOrder);
		if (!resultDO.isSuccess()) {
			return resultDO;
		}

		// 更新拍摄表状态（拍摄状态为取消）
		WorkOrderPhotographyDO photographyDO = new WorkOrderPhotographyDO();
		photographyDO.setSeatID(workOrderBook.getSeatID());
		photographyDO.setWorkOrderID(workOrderBook.getWorkOrderID());
		photographyDO.setShotStatus(BonConstant.SHOT_STATUS_CHANGEDATE); // 0-未拍,1-已拍,2-重拍,3-补拍,4-改期,-1取消
		photographyDO.setIsDelete(BonConstant.DB_DELETE);
		photographyDO.setModifiID(domain.getModifiID());
		photographyDO.setModifi(domain.getModifi());
		resultDO = workPhotographyService.edit(photographyDO);
		if (!resultDO.isSuccess()) {
			return resultDO;
		}

		// 写入任务记录表
		WorkCommentDO commentDO = new WorkCommentDO();
		commentDO.setWorkOrderID(workOrderBook.getWorkOrderID());
		commentDO.setProcessInstanceID(workOrderBook.getProcessInstanceID());
		commentDO.setTaskDefID(TaskDefKey.UPLOAD_PHOTO.getKey()); // 订单上摄控
		commentDO.setTaskName(TaskDefKey.UPLOAD_PHOTO.getName());
		commentDO.setTaskID(""); // 从acitivti获取
		commentDO.setType(BonConstant.COMMENT_TYPE_EVENT); // 0备注；1事件
		commentDO.setRemark("预约工单改期");
		commentDO.setFlag(0); // 0普通；1特殊
		commentDO.setAuthorID(domain.getModifiID());
		commentDO.setAuthor(domain.getModifi());
		workCommentService.add(commentDO);

		return result;
	}

	@Override
	public ResultDO exchangeDate(WorkOrderExchangeDO domain) {
		// 工单调换
		ResultDO result = new ResultSupport();
		// 1.数据验证
		if (isNumberInValid(domain.getId1()) || isNumberInValid(domain.getId2())) {
			result.setErrorCode(ResultDO.PARAM_ERROR);
			result.setErrorMsg("参数错误");
			result.setSuccess(false);
			return result;
		}
		// 查询工单预约表
		WorkOrderBook workOrderBook1 = workOrderBookMapper.selectByPrimaryKey(domain.getId1());
		WorkOrderBook workOrderBook2 = workOrderBookMapper.selectByPrimaryKey(domain.getId2());
		if (null == workOrderBook1 || null == workOrderBook2) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("未找到工单预订信息");
			result.setSuccess(false);
			return result;
		}

		// 查询工单拍摄表
		WorkOrderPhotographyExample photographyExample = new WorkOrderPhotographyExample();
		WorkOrderPhotographyExample.Criteria photographyCriteria = photographyExample.createCriteria();
		List<Integer> seatIdList = new ArrayList<Integer>();
		seatIdList.add(workOrderBook1.getSeatID());
		seatIdList.add(workOrderBook2.getSeatID());
		photographyCriteria.andSeatIDIn(seatIdList);
		photographyCriteria.andShotStatusEqualTo(BonConstant.SHOT_STATUS_NORMAL); // 只查询未拍摄的订单（已拍摄、已取消的不允许调换）
		photographyCriteria.andIsDeleteEqualTo(BonConstant.DB_NORMAL);
		List<WorkOrderPhotography> photographyList = workOrderPhotographyMapper.selectByExample(photographyExample);
		if (null == photographyList || photographyList.size() < 2) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("未找到有效的工单拍摄信息");
			result.setSuccess(false);
			return result;
		}

		// 更新工单1
		WorkOrderBook workOrderBook = new WorkOrderBook();
		workOrderBook.setId(workOrderBook1.getId());
		workOrderBook.setSiteSeatID(workOrderBook2.getSiteSeatID());
		workOrderBook.setSeatID(workOrderBook2.getSeatID());
		workOrderBook.setSchedulingDate(workOrderBook2.getSchedulingDate());
		workOrderBook.setModifiID(domain.getModifiID());
		workOrderBook.setModifi(domain.getModifi());
		workOrderBook.setModifitime(Calendar.getInstance().getTime());
		workOrderBookMapper.updateByPrimaryKeySelective(workOrderBook);
		// 更新工单2
		workOrderBook.setId(workOrderBook2.getId());
		workOrderBook.setSiteSeatID(workOrderBook1.getSiteSeatID());
		workOrderBook.setSeatID(workOrderBook1.getSeatID());
		workOrderBook.setSchedulingDate(workOrderBook1.getSchedulingDate());
		workOrderBookMapper.updateByPrimaryKeySelective(workOrderBook);

		// 更新工单1拍摄记录
		WorkOrderPhotographyDO photographyDO = new WorkOrderPhotographyDO();
		photographyDO.setId(photographyList.get(0).getId());
		photographyDO.setSiteSeatID(photographyList.get(1).getSiteSeatID());
		photographyDO.setSeatID(photographyList.get(1).getSeatID());
		photographyDO.setPlanDate(photographyList.get(1).getPlanDate());
		photographyDO.setModifiID(domain.getModifiID());
		photographyDO.setModifi(domain.getModifi());
		workPhotographyService.edit(photographyDO);
		// 更新工单2拍摄记录
		photographyDO.setId(photographyList.get(1).getId());
		photographyDO.setSiteSeatID(photographyList.get(0).getSiteSeatID());
		photographyDO.setSeatID(photographyList.get(0).getSeatID());
		photographyDO.setPlanDate(photographyList.get(0).getPlanDate());
		workPhotographyService.edit(photographyDO);

		return result;
	}

	@Override
	public ResultDO cancel(WorkOrderBookDO domain) {
		// 工单取消
		ResultDO result = new ResultSupport();
		// 1.数据验证
		if (null == domain.getIds() || domain.getIds().length < 1) {
			result.setErrorCode(ResultDO.PARAM_ERROR);
			result.setErrorMsg("参数错误");
			result.setSuccess(false);
			return result;
		}

		// 查询工单预约表
		WorkOrderBookExample example = new WorkOrderBookExample();
		WorkOrderBookExample.Criteria criteria = example.createCriteria();
		criteria.andIdIn(Arrays.asList(domain.getIds()));
		List<WorkOrderBook> list = workOrderBookMapper.selectByExample(example);
		if (null != list && list.size() < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("未找到该席位的预订信息");
			result.setSuccess(false);
			return result;
		}

		// 席位
		List<Integer> seatIds = new ArrayList<Integer>();
		List<Integer> workOrderIds = new ArrayList<Integer>();
		for (WorkOrderBook data : list) {
			seatIds.add(data.getSeatID());
			workOrderIds.add(data.getWorkOrderID());
		}

		// 更新工单预约表（该工单拍摄状态变为“未预约“）
		WorkOrderBook serviceEntity = new WorkOrderBook();
		serviceEntity.setIsDelete(BonConstant.DB_DELETE);
		serviceEntity.setStatus(BonConstant.STATUS_INVALID);
		serviceEntity.setModifiID(domain.getModifiID());
		serviceEntity.setModifi(domain.getModifi());
		serviceEntity.setModifitime(Calendar.getInstance().getTime());

		WorkOrderBookExample serviceExample = new WorkOrderBookExample();
		WorkOrderBookExample.Criteria serviceCriteria = serviceExample.createCriteria();
		serviceCriteria.andSeatIDIn(seatIds);

		workOrderBookMapper.updateByExampleSelective(serviceEntity, serviceExample);

		// 更新工单表（ 状态：未预约）
		WorkOrder orderEntity = new WorkOrder();
		// orderEntity.setIsDelete(BonConstant.DB_DELETE);
		orderEntity.setOrderStatus(BonConstant.ORDER_STATUS_CANCEL); // 取消预约
		orderEntity.setTaskDefID(TaskDefKey.ORDER_SCHEDULE.getKey()); // 待重新预约
		orderEntity.setModifiID(domain.getModifiID());
		orderEntity.setModifi(domain.getModifi());
		orderEntity.setModifitime(Calendar.getInstance().getTime());

		WorkOrderExample orderExample = new WorkOrderExample();
		WorkOrderExample.Criteria orderCriteria = orderExample.createCriteria();
		orderCriteria.andIdIn(workOrderIds);

		workOrderMapper.updateByExampleSelective(orderEntity, orderExample);

		// 更新席位表（状态：可用）
		WorkSeatScheduling seatEntity = new WorkSeatScheduling();
		seatEntity.setStatus(BonConstant.SEAT_STATUS_ENABLE); // 席位状态为可用
		seatEntity.setModifiID(domain.getModifiID());
		seatEntity.setModifi(domain.getModifi());
		seatEntity.setModifitime(Calendar.getInstance().getTime());

		WorkSeatSchedulingExample seatExample = new WorkSeatSchedulingExample();
		WorkSeatSchedulingExample.Criteria seatCriteria = seatExample.createCriteria();
		seatCriteria.andIdIn(seatIds);

		workSeatSchedulingMapper.updateByExampleSelective(seatEntity, seatExample);

		// 更新拍摄表（拍摄状态为：取消）
		workPhotographyService.delete(seatIds);

		return result;
	}

}
